package com.controller;

import com.Service.UserPermissionsService;
import com.model.pojo.Menu;
import com.model.pojo.User;
import com.model.pojo.UserPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("UserPermissions")
public class UserPermissionsController {
    @Resource
    private UserPermissionsService userPermissionsService;
    @Resource
    private UserPermissions userPermissions;

    @RequestMapping("index")
    public String index(HttpSession session) {

        User user = (User) session.getAttribute("user");//获取user信息
        List<UserPermissions> menuNameOne = userPermissionsService.menuNameOne(user.getUserName());//通过user名 获取可用一级菜单

        List<Menu> menuPathList = userPermissionsService.menuPath(user.getUserName());//通过user名获取可用二级菜单和菜单点击地址
        session.setAttribute("menuNameOne", menuNameOne); //传值到index
        session.setAttribute("menuPathList", menuPathList);


        return "test";

    }


    @RequestMapping("userNames")
    public String userNames(HttpSession session) {//权限管理设置--查询所有使用人


        List<User> userNames = userPermissionsService.userNames();


        session.setAttribute("userNames", userNames);//跳转展示所有人


        return "userNames";

    }

    @RequestMapping("permissions")
    public String updatePermissions(HttpSession session, String name) {


        List<UserPermissions> userPermissionsList = userPermissionsService.userPermissionsList(name);//通过姓名查询可使用的菜单并返回 页面复选框打√
        session.setAttribute("name", name);
        session.setAttribute("userPermissionsList", userPermissionsList);


        return "PermissionsList";

    }

    @RequestMapping("messagesubmit")
    public String messageSubmit(HttpSession session, HttpServletRequest request) {
        boolean bl = false;
        String[] menu = request.getParameterValues("lll");//获取所有lll的值
        String name = (String) session.getAttribute("name");//获取当前被修改权限名称
        List<User> userNames = (List) session.getAttribute("userNames");//获取所有使用者信息
        for (int i = 0; i < userNames.size(); i++) {
            if (name.equals(userNames.get(i).getUserName())) {//如果姓名相同  将使用者权限传值判断 如果是管理员 不可被更改 其余可更改

                bl = userPermissionsService.updaPermissions(name, userNames.get(i).getUserPermissions(), menu, session);
                break;
            }

        }

        if (bl == true) {
            session.setAttribute("guan", "成功");
        }


        return "redirect:userNames";

    }


}
